﻿using System.Net;
using System.Reflection;
namespace System.Web.Hosting
{
    public partial class WebHostWorkerRequest : NetHttpWorkerRequest
    {
        private WeakReference _webHostRuntime;
        private WeakReference _netContext;
        private INetHttpHandler _remapHandler;
        private bool _closed;

        //public WebHostWorkerRequest() { }
        internal WebHostWorkerRequest(IWebHostRuntime webHostRuntime, HttpListenerContext netContext)
            : base(netContext)
        {
            _webHostRuntime = new WeakReference(webHostRuntime);
            _netContext = new WeakReference(netContext);
        }

        internal IWebHostRuntime WebHostRuntime
        {
            get { return (_webHostRuntime.IsAlive ? (IWebHostRuntime)_webHostRuntime.Target : null); }
        }

        internal HttpListenerContext NetContext
        {
            get { return (_netContext.IsAlive ? (HttpListenerContext)_netContext.Target : null); }
        }

        internal virtual void Close()
        {
            CloseConnectionCore();
        }

        public override void CloseConnection()
        {
            CloseConnectionCore();
        }

        internal int CloseConnectionCore()
        {
            var netContext = NetContext;
            if ((netContext == null) || (_closed == true))
                return 0;
            _closed = true;
            var r = netContext.Response;
            r.Close();
            return 0;
        }

        //public override void ProcessRequest()
        //{
        //    Console.WriteLine("Here");
        //}
        //{
        //    var r = _context.Response;
        //    //_urlRoutingModule.PostResolveRequestCache(_context);
        //    //if (_remapHandler != null)
        //    //{
        //    //    var remapHandler = _remapHandler;
        //    //    _remapHandler = null;
        //    //    var mvcHandler = (remapHandler as MvcHandler);
        //    //    if (mvcHandler != null)
        //    //    {
        //    //        var processRequestMethod = typeof(MvcHandler).GetMethod("ProcessRequest", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(HttpContextBase) }, null);
        //    //        try { processRequestMethod.Invoke(mvcHandler, new[] { _context }); }
        //    //        catch (TargetInvocationException ex) { r.Write(ex.InnerException.Message); }
        //    //    }
        //    //}
        //}

        public void SetRemapHandler(string handlerType, string handlerName)
        {
            //var type = Type.GetType(handlerType);
            //switch (type.Name)
            //{
            //    case "MvcHandler": type = typeof(Mvc.MvcNetHandler); break;
            //    case "MvcRouteHandler": type = typeof(Mvc.MvcRouteNetHandler); break;
            //}
            //if (type != null)
            //    _remapHandler = (INetHttpHandler)Activator.CreateInstance(type);
        }


        //static NetHttpWorkerRequest()
        //{
        //    DefaultControllerFactoryBuildManagerKludge();
        //    MvcSetup();
        //}

        //private static UrlRoutingModule _urlRoutingModule = new UrlRoutingModule();

        //private static void MvcSetup()
        //{
        //    var routes = RouteTable.Routes;
        //    routes.MapRoute(
        //        "Default", // Route name
        //        "{controller}/{action}/{id}", // URL with parameters
        //        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        //    );
        //}

        //private static void DefaultControllerFactoryBuildManagerKludge()
        //{
        //    // inject new BuildManager
        //    var controllerFactory = ControllerBuilder.Current.GetControllerFactory();
        //    if (controllerFactory is DefaultControllerFactory)
        //    {
        //        var builder = new NetBuildManager(() => new[] { typeof(NetHttpWorkerRequest).Assembly });
        //        typeof(DefaultControllerFactory).GetProperty("BuildManager", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(controllerFactory, builder, null);
        //    }
        //}
    }
}
